﻿/* Default.aspx.cs ----------------------------------------------------------
 *
 * Informatix Football Apps
 * QB Passer Rating ASP.NET
 *
 * © 2013 Jason Barkes - http://jbarkes.blogspot.com
 ----------------------------------------------------------------------------*/

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace QBRatingServiceClient
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtComps.Focus();
        }

        protected void btnCalc_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                // Get the selected formula
                QBRatingService.QBRatingFormula formula = 
                    (QBRatingService.QBRatingFormula)Enum.Parse(typeof(QBRatingService.QBRatingFormula), ddlFormula.Text);

                // Create the rating service
                QBRatingService.QBRatingServiceClient svc = new QBRatingService.QBRatingServiceClient();

                // Call the service to get the rating
                QBRatingService.QBRating qb = svc.GetRating(
                    formula,
                    Convert.ToInt32(txtComps.Text),
                    Convert.ToInt32(txtAtts.Text),
                    Convert.ToInt32(txtYards.Text),
                    Convert.ToInt32(txtTds.Text),
                    Convert.ToInt32(txtInts.Text),
                    QBRatingService.WebMessageFormat.Xml);

                // Close the service connection
                svc.Close();

                // Show the results
                ToggleCalcStats(true);
                ShowResults(qb);
            }
        }             

        protected void btnReset_Click(object sender, EventArgs e)
        {
            ToggleCalcStats(false);

            ddlFormula.SelectedIndex = 0;

            foreach (Control ctl in pnlStats.Controls)
            {
                if (ctl.ID != null && ctl is TextBox)
                {
                    (ctl as TextBox).Text = "";
                }
            }
        }

        private void ToggleCalcStats(bool show)
        {
            lblCompletionPercentage.Visible = show;
            lblYardsPerAttempt.Visible = show;
            lblTouchdownPercentage.Visible = show;
            lblInterceptionPercentage.Visible = show;
        }

        private void ShowResults(QBRatingService.QBRating stats)
        {
            lblCompletionPercentage.Text = stats.CompletionPercentage.ToString() + "%";
            lblYardsPerAttempt.Text = stats.YardsPerAttempt.ToString() + " per attempt";
            lblTouchdownPercentage.Text = stats.TouchdownPercentage.ToString() + "%";
            lblInterceptionPercentage.Text = stats.InterceptionPercentage.ToString() + "%";
            txtRating.Text = stats.Rating.ToString();
        }
    }
}